package com.mycat.web.controller;

import java.util.Arrays;

/**
 * Created by Administrator on 2017/2/24.
 */
public class test {
    public static void main(String[] args) {

        int[] a = new int[]{1, 5, 3, 8, 7, 6, 9};

        int[] b = selectSort(a);

        int[] c = bubbleSort(a);

        int[] d = insertSort(a);

        int[] aa = {5, 4, 2, 4, 9, 1};
        Arrays.sort(aa);  //进行排序
        for (int i : aa) {
            System.out.print(i);
        }
    }


    public static int[] selectSort(int[] args) {//选择排序算法
        for (int i = 0; i < args.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < args.length; j++) {
                if (args[min] > args[j]) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = args[i];
                args[i] = args[min];
                args[min] = temp;
            }
        }
        return args;
    }

    public static int[] bubbleSort(int[] args) {//冒泡排序算法
        for (int i = 0; i < args.length - 1; i++) {
            for (int j = i + 1; j < args.length; j++) {
                if (args[i] > args[j]) {
                    int temp = args[i];
                    args[i] = args[j];
                    args[j] = temp;
                }
            }
        }
        return args;
    }

    public static int[] insertSort(int[] args) {//插入排序算法
        for (int i = 1; i < args.length; i++) {
            for (int j = i; j > 0; j--) {
                if (args[j] < args[j - 1]) {
                    int temp = args[j - 1];
                    args[j - 1] = args[j];
                    args[j] = temp;
                } else break;
            }
        }
        return args;
    }
}
